草庐IT

C++11 auto 和 size_type

全部标签

c++11:从模板函数构建 std::tuple

我有以下功能:templateTCheck(intindex);我如何编写函数CheckTuple,它在给定元组类型的情况下通过调用Check来填充元组?p>例如:CheckTuple>()将返回以下元组:std::make_tuple(Check(1),Check(2),Check(3))我看到的其他问题涉及解包一个给定的元组,而不是用这种方式构建一个元组。 最佳答案 使用C++14的integer_sequence实现您正在寻找的东西变得非常简单.如果您没有可用的,here'saC++11implementation由Jonat

c++11 注册缓存线程安全

在volatile:TheMultithreadedProgrammer'sBestFriend,AndreiAlexandrescu给出了这个例子:classGadget{public:voidWait(){while(!flag_){Sleep(1000);//sleepsfor1000milliseconds}}voidWakeup(){flag_=true;}...private:boolflag_;};他说,...thecompilerconcludesthatitcancacheflag_inaregister...itharmscorrectness:afteryouca

c++ - 我想我可以理解 N4140 中的 §5.3.4/11,但是扩展的分配函数的概念对我来说是不可理解的

以下两段是从N4140复制的(重点是我的)。§5.3.4/11:Whenanew-expressioncallsanallocationfunctionandthatallocationhasnotbeenextended,thenew-expressionpassestheamountofspacerequestedtotheallocationfunctionasthefirstargumentoftypestd::size_t.Thatargumentshallbenolessthanthesizeoftheobjectbeingcreated;itmaybegreatertha

c++ - 从 std::true_type 继承 vs static constexpr const bool 成员

我知道这不是一个非常尖锐的问题。使用一个比另一个有优势(编译时间、依赖性、调试符号大小、可用性、可读性等)吗?templatestructIsSharedPtr:std::false_type{};对比templatestructIsSharedPtr{staticconstexprboolvalue=false;};相关问题...templatestructS;templatestructS{};templatestructS{};对比templatestructS;templatestructS{};templatestructS{}; 最佳答案

删除说明符与私有(private)函数的 C++11 用法

我正在强化我的C++(例如,尝试进入更现代风格的编码)并且正在查看删除说明符。据我了解,它用于确保无法定义或调用某些功能。如果我理解正确的话,这主要是在赋值和复制的范围内。我不太确定使用delete说明符和仅将这些函数设为私有(private)之间有什么区别。例如,有什么区别:classFoo{private:Foo&operator(constFoo&);Foo(constFoo&);};和classBar{public:Bar&operator(constBar&)=delete;Bar(constBar&)=delete;};换句话说:使用delete说明符有什么好处?仅仅是为了

c++ - G++ 4.4.7 中的 "names the constructor, not the type"

我有以下简单的C++代码:#includeclassA{public:A(inty):x(y){}A&operator=(constA&rhs);intx;};A::A&A::operator=(constA&rhs){this->x=rhs.x;return*this;}intmain(int,char**){Aa1(5);Aa2(4);printf("a2.x==%d\n",a2.x);a2=a1;printf("a2.x==%d\n",a2.x);return0;}第11行,A的operator=()函数的定义所在,格式不正确......或者,至少,我相信是这样。正如预期的那样,

c++ - 前向声明的类型和 "non-class type as already been declared as a class type"

我对以下代码有疑问:templatevoidfoo(structbar&b);structbar{};intmain(){}它在GCC上编译成功,但在MSVC(2008)上编译失败并出现以下错误:C2990:“bar”:已声明为类类型的非类类型是代码错误还是MSVC中的错误?如果我在模板定义之前添加structbar;就可以了。 最佳答案 我们有我们的赢家:https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-

c++ - std::function<> 和 Intel 编译器版本 11.1

我在使用英特尔编译器中的lambda函数时遇到问题,特别是以下代码无法编译:templatestd::functionmake_func(Tx){return[=](intindex)->T{returnx;};}我得到的错误是error:namespace"std"hasnomember"function"代码在我的Mac上编译和运行良好(macportsgcc版本4.5)。错误在起作用,我们使用的是Intel编译器版本11.1。它确实接受lambda函数(使用-std=c++0x选项),例如:autolam=[=](intj)->int{printf("testingforlamb

c++ - 错误 C2678 : binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion)

我正在尝试对每个元素中包含一个int和一个字符串的vector进行排序。它是一个称为vector食谱的类类型的vector。出现上述错误,这是我的代码:在我的Recipe.h文件中structRecipe{public:stringget_cname()const{returnchef_name;}private:intrecipe_id;stringchef_name;在我的Menu.cpp文件中voidMenu::show()const{sort(recipes.begin(),recipes.end(),Sort_by_cname());}在我的Menu.h文件中#include

C++11 可变参数模板模板参数

保留老问题。请参阅下面的解决方案。这可能很简单,但仍然如此。我有以下C++11代码片段:#includetemplatestructtypelist{};templatestructEventContainer{typedefTType;///TODO.Ringbufferstd::vectorcontainer;voidpush(constT&t){EventContainer::container.push_back(t);}virtual~EventContainer(){}};templateclassTL>classEventStorage:publicEventContai